﻿using AnlianCommon.Common.Domains.BeginningManagement;
using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using MediatR;
using Microsoft.EntityFrameworkCore;
using NPOI.HSSF.Record;

namespace SheepCommon.API.SheepCommands
{
    public class SheepEarBuglesBackFillHandler : IRequestHandler<SheepEarBuglesBackFillCommand, Result>
    {
        private readonly IBaseRepository<SheepArchives> _sheepArchivesRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieldRepository;
        private readonly IBaseRepository<Startofseason> _startofseasonRepository;

        public SheepEarBuglesBackFillHandler(IBaseRepository<SheepArchives> sheepArchivesRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository, IBaseRepository<Startofseason> startofseasonRepository)
        {
            _sheepArchivesRepository = sheepArchivesRepository;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
            _startofseasonRepository = startofseasonRepository;
        }
        /// <summary>
        /// 通过耳号选择下拉加载档案信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SheepEarBuglesBackFillCommand request, CancellationToken cancellationToken)
        {
            var result = new Result();
            try
            {
                var sheepEarBugles = await (from a in await _sheepArchivesRepository.GetAll()
                                            join b in await _cottageRepository.GetAll() on a.CottageId equals b.CottageId
                                            join c in await _fieldRepository.GetAll() on a.FieldId equals c.FieldId
                                            join d in await _startofseasonRepository.GetAll() on a.RecordAuricle equals d.Auricle
                                            where a.RecordAuricle == request.RecordAuricle
                                            select new
                                            {
                                                a.RecordId,
                                                a.RecordAuricle,
                                                a.RecordCorpuscle,
                                                a.RecordMother,
                                                a.RecordFather,
                                                a.RecordBreed,
                                                a.RecordBorn,
                                                a.RecordDate,
                                                a.RecordSite,
                                                a.RecordWeight,
                                                a.RecordRemark,
                                                a.AdmissionDate,
                                                a.Sex,
                                                a.Gene,
                                                a.RecordType,
                                                a.FarmId,
                                                a.FieldId,
                                                a.CottageId,
                                                a.SheepImage,
                                                a.Stage,
                                                a.ProductionGrade,
                                                a.SalesGrade,
                                                a.ReproductiveState,
                                                a.ReproductiveStateDays,
                                                a.PresenceStatus,
                                                a.IsPregnancyTest,
                                                a.WeanWeight,
                                                a.WeanDate,
                                                b.CottageName,
                                                c.FieldName,
                                                d.Weight,
                                            }).FirstOrDefaultAsync();
                if (sheepEarBugles != null)
                {
                    result.Data = sheepEarBugles;
                }

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
